


import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_pleasure_video/constant/color.dart';
import 'package:flutter_pleasure_video/page/personal/personal_page.dart';
import 'package:flutter_pleasure_video/page/source/video_source_page.dart';
import 'package:flutter_pleasure_video/util/screen_util.dart';
import 'package:get/get.dart';

import 'home/tab_home_page.dart';


// ignore: must_be_immutable
class HomePage extends StatelessWidget {

  bool _initScreenBool = false;
  final pageController = PageController();
  List? pages;
  BottomBarController barController=BottomBarController();
  bool isSetPageControllerPage=true;

  @override
  Widget build(BuildContext context) {
    _initScreen(context);
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark, //黑色是dark 白色是light
      child:Scaffold(
        appBar: null,
        body: Container(
          width: ScreenUtil.instance.width,
          height: ScreenUtil.instance.height,
          color: AppColor.white,
          padding: EdgeInsets.only(top: ScreenUtil.instance.statusBarHeight),
          child: PageView.builder(
            itemBuilder: (BuildContext context,int index) {
              return pages![index];
            },
            itemCount: 3,
            controller: pageController,
            // 提前预加载当前pageView的下一个视图
            allowImplicitScrolling: true,
            physics: NeverScrollableScrollPhysics(), // 禁止滑动
          ),
        ),
        bottomNavigationBar: Obx(
          (){
            if(pageController.page!=null&&isSetPageControllerPage){
              barController.setBottomBarCurrentIndex(pageController.page!.round());
            }
            isSetPageControllerPage=true;
            return BottomNavigationBar( // 底部导航
              items: <BottomNavigationBarItem>[
                BottomNavigationBarItem(icon: Icon(Icons.home), label: "Home"),
                BottomNavigationBarItem(icon: Icon(Icons.business), label: "Business"),
                BottomNavigationBarItem(icon: Icon(Icons.school), label: "School"),
              ],
              currentIndex: barController.currentIndex.value,
              fixedColor: Colors.blue,
              onTap: _onClickBottomBar,
            );
          },
        ),
      ),
    );
  }

  // 初始化获取屏幕数据
  void _initScreen(BuildContext context){
    if(pages==null||pages!.length<1) {
      pages = [
        TabHomePage(_setIsAutoPlayBanner),
        VideoSourcePage(),
        PersonalPage(),
      ];
    }
    if (_initScreenBool == false) {
      double screenBottom = MediaQuery.of(context).padding.bottom;
      Size screenSize = MediaQuery.of(context).size;
      ScreenUtil.init(
          width: screenSize.width,
          height: screenSize.height,
          maxPhysicalSize: screenSize.width,
          bottomHeight: screenBottom);
      _initScreenBool = true;
    }
  }

  _onClickBottomBar(index){
    isSetPageControllerPage=false;
    _setAutoPlayBanner(index==0);
    pageController.animateToPage(
        index,
        duration: Duration(milliseconds: 250),
        curve: Cubic(1.0, 1.0, 1.0, 1.0)
    );
    barController.setBottomBarCurrentIndex(index);
  }

  //是否自动播放banner的图片切换
  void _setAutoPlayBanner(bool isPlay) {
    if (_isAutoPlayBannerListener != null) {
      _isAutoPlayBannerListener!(isPlay);
    }
  }

  void Function(bool isPlay)? _isAutoPlayBannerListener;

  void _setIsAutoPlayBanner(Function(bool isPlay) call) {
    this._isAutoPlayBannerListener = call;
  }
}


class BottomBarController extends GetxController{
  var currentIndex=0.obs;

  setBottomBarCurrentIndex(int index)=>currentIndex.value=index;
}
